<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>商品管理</cite></a>
    <a><cite>肓盒商品</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_blind_box_search">
        <script type="text/html" template>
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入关键字">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_blind_box_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_blind_box_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
            <a lay-href="blind_box/form" class="layui-btn layui-btn-normal">
              <i class="layui-icon layui-icon-add-1"></i>添加肓盒
            </a>
          </div>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_blind_box"></div>
      <script type="text/html" id="tpl_status">
        {{# if (d.status.value === 'init') { }}
        <span class="layui-font-gray">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'on_sale') { }}
        <span class="layui-font-green">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'pause') { }}
        <span class="layui-font-gray">{{d.status.description}}</span>
        {{# } }}
        {{# if (d.status.value === 'stop') { }}
        <span class="layui-font-red">{{d.status.description}}</span>
        {{# } }}
      </script>
      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link" lay-href="blind_box/detail/id={{d.id}}">
          肓盒详情
        </a>
        <div class="ant-divider"></div>
        <a class="layui-btn layui-btn-xs" lay-event="more">更多操作<i class="layui-icon layui-icon-down"></i></a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table', 'dropdown'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , admin = layui.admin
      , form = layui.form
      , view = layui.view
      , dropdown = layui.dropdown

    let resourceUrl = 'agent-admin/blind_boxes'

    // 列表查询
    fox_table.render({
      id: 'table_blind_box'
      , url: resourceUrl
      , cols: [[
        {title: 'ID', field: 'id', width: 80, align: "center", sort: true}
        , {title: '肓盒名称', field: 'name', minWidth: 200}
        , {title: '排序', field: 'sequence', width: 80, align: "center"}
        , {title: '发行量', field: 'issue_count', width: 80}
        , {title: '剩余量', field: 'left_count', width: 80}
        , {title: '销售量', field: 'sales_count', width: 80}
        , {title: '状态', templet: '#tpl_status', width: 100}
        , {title: '添加时间', field: 'created_at', width: 180, align: "center"}
        , {title: '操作', width: 250, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });

    //监听操作栏
    fox_table.toolbar(function (obj) {
      let data = obj.data;
      if (obj.event === 'more') {
        //更多下拉菜单
        let options = [{
          title: '盲盒信息编辑',
          id: 'edit'
        }, {
          title: '提前购设置',
          id: 'early_buy'
        }];
        if (data.status.value === 'on_sale') {
          options.push({
            title: '下架',
            id: 'offline',
          })
        } else if (data.status.value === 'init' || data.status.value === 'pause') {
          options.push({
            title: data.status.value === 'init' ? '上架' : '重新上架',
            id: 'online',
          })
          options.push({
            title: '停售',
            id: 'stop',
          })
        }
        dropdown.render({
          elem: this
          , show: true //外部事件触发即显示
          , data: options
          , click: function (opt, othis) {
            //根据 id 做出不同操作
            if (opt.id === 'offline') {
              layer.confirm('确定下架该产品?', function (index) {
                admin.post(resourceUrl + '/' + data.id + '/offline', {}, function (res) {
                  layer.close(index);
                  fox_table.reload(); //重载表格
                })
              });
            } else if (opt.id === 'online') {
              layer.confirm('确定上架该肓盒?', function (index) {
                admin.post(resourceUrl + '/' + data.id + '/online', {}, function (res) {
                  layer.close(index);
                  fox_table.reload(); //重载表格
                })
              });
            } else if (opt.id === 'product_edit') {
              showEditForm(data)
            } else if (opt.id === 'offline') {
              layer.confirm('确定下架该肓盒?肓盒下架后仅用户端不可见，锁定的库存不会释放', function (index) {
                admin.post(resourceUrl + '/' + data.id + '/offline', {}, function (res) {
                  layer.close(index);
                  fox_table.reload(); //重载表格
                })
              });
            } else if (opt.id === 'early_buy') {
              showEarlyBuyList(data)
            } else if (opt.id === 'edit') {
              showEditForm(data);
            } else if (opt.id === 'stop') {
              layer.confirm('确定停售该肓盒?肓盒停售锁定的商品库存将自动释放，该肓盒不能再上架销售', function (index) {
                admin.post(resourceUrl + '/' + data.id + '/stop', {}, function (res) {
                  layer.close(index);
                  fox_table.reload(); //重载表格
                })
              });
            } else {
              layer.msg('开发中，敬请期待');
            }
          }
          , align: 'right' //右对齐弹出（v2.6.8 新增）
          , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
        });
      }
    });

    //搜索
    form.on('submit(submit_blind_box_search)', function (d) {
      fox_table.reload(d.field);
      return false;
    });
    //重置
    form.on('submit(reset_blind_box_search)', function (d) {
      $("input[name='keyword']").val('');
      fox_table.reload({});
      return false;
    });

    function showEditForm(data) {
      let minWidth = '800px';
      let minHeight = '600px';
      admin.popup({
        title: '肓盒修改'
        , area: [minWidth, minHeight]
        , id: 'popup_blind_box'
        , success: function (layero, index) {
          view(this.id).render('/blind_box/basic_form', data).done(function () {
            form.on('submit(submit_basic)', function (d) {
              let field = d.field;
              field.show_left_count = field.show_left_count === 'on' ? 1 : 0;
              admin.post(resourceUrl + '/' + data.id + '/basic', field, function (res) {
                layer.close(index);
                fox_table.reload();
              })
            })
          })
        }
      })
    }

    //提前购设置
    function showEarlyBuyList(data) {
      let minWidth = '800px';
      let minHeight = '880px';
      admin.popup({
        title: '提前购用户清单'
        , area: [minWidth, minHeight]
        , id: 'popup_early_buy'
        , success: function (layero, index) {
          view(this.id).render('/product/early_buy', data).done(function () {
          })
        }
      })
    }
  })
</script>

